import request from './axios'

export const activityApi = {
  // 获取活动列表
  getActivities(params = {}) {
    const userId = localStorage.getItem('userId')
    return request({
      url: '/activity/activities/',
      method: 'get',
      params: {
        ...params,
        user_id: userId
      }
    })
  },

  // 获取活动详情
  getActivityDetail(activityId) {
    const userId = localStorage.getItem('userId')
    return request({
      url: `/activity/activities/${activityId}/`,
      method: 'get',
      params: { user_id: userId }
    })
  },

  // 创建活动
  createActivity(data) {
    return request({
      url: '/activity/activities/',
      method: 'post',
      data: {
        ...data,
        creator_id: parseInt(localStorage.getItem('userId'))
      }
    })
  },

  // 更新活动
  updateActivity(activityId, data) {
    return request({
      url: `/activity/activities/${activityId}/`,
      method: 'put',
      data: {
        ...data,
        creator_id: parseInt(localStorage.getItem('userId'))
      }
    })
  },

  // 删除活动
  deleteActivity(activityId) {
    return request({
      url: `/activity/activities/${activityId}/`,
      method: 'delete',
      data: {
        creator_id: parseInt(localStorage.getItem('userId'))
      }
    })
  },

  // 参加/取消参加活动
  toggleJoin(activityId, note = '') {
    return request({
      url: `/activity/activities/${activityId}/join/`,
      method: 'post',
      data: {
        user_id: parseInt(localStorage.getItem('userId')),
        note
      }
    })
  },

  // 获取活动参与者
  getParticipants(activityId, params = {}) {
    return request({
      url: `/activity/activities/${activityId}/participants/`,
      method: 'get',
      params
    })
  },

  // 收藏/取消收藏活动
  toggleFavorite(activityId) {
    return request({
      url: `/activity/activities/${activityId}/favorite/`,
      method: 'post',
      data: {
        user_id: parseInt(localStorage.getItem('userId'))
      }
    })
  },

  // 获取活动评论
  getComments(activityId, params = {}) {
    return request({
      url: `/activity/activities/${activityId}/comments/`,
      method: 'get',
      params
    })
  },

  // 发表评论
  addComment(activityId, content, parentId = null) {
    return request({
      url: `/activity/activities/${activityId}/comments/`,
      method: 'post',
      data: {
        user_id: parseInt(localStorage.getItem('userId')),
        content,
        parent_id: parentId
      }
    })
  }
}